using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Threading;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    internal class FormDate : CMFormElement
    {
        public override string Value
        {
            get
            {
                TextBox textbox = this.FindControl(string.Format("textbox_{0}", this.ID)) as TextBox;
                return textbox.Text;
            }
        }

        internal FormDate(Forms.Sdk.Items.Date element) : base(element)
        {
            PlaceHolder calendarControls = new PlaceHolder();

            System.Web.UI.WebControls.TextBox textbox = new TextBox();
            textbox.ID = string.Format("textbox_{0}", this.ID);

            calendarControls.Controls.Add(textbox);

            ImageButton button = new ImageButton();
            button.ID = string.Format("button_calendar_{0}", this.ID);
            
            calendarControls.Controls.Add(button);

            AjaxControlToolkit.CalendarExtender calendar = new AjaxControlToolkit.CalendarExtender();
            calendar.Animated = true;
            calendar.PopupPosition = AjaxControlToolkit.CalendarPosition.Right;
            calendar.TargetControlID = textbox.ID;
            calendar.PopupButtonID = button.ID;
            calendar.Format = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

            if (element.UseNow)
            {
                calendar.SelectedDate = DateTime.Now;
            }

            if (element.DefaultValue.HasValue)
            {
                calendar.SelectedDate = element.DefaultValue.Value;
            }

            calendarControls.Controls.Add(calendar);

            Label label = new Label();
            label.Text = element.Label;
            label.CssClass = string.Format("{0}Label", element.CSS);

            #region Validators

            PlaceHolder phValidators = new PlaceHolder();
            
            //Required
            if (element.Required)
            {
                Label redStar = new Label();
                redStar.ForeColor = Color.Red;
                redStar.Text = "*";

                RequiredFieldValidator required = new RequiredFieldValidator();
                required.ID = string.Format("required_{0}_{1}", element.FormID, element.ID);
                required.ControlToValidate = textbox.ID;
                required.ValidationGroup = element.FormID.ToString();
                required.Display = ValidatorDisplay.Dynamic;
                required.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label); 
            
                phValidators.Controls.Add(redStar);
                phValidators.Controls.Add(required);
            }

            RangeValidator range = new RangeValidator();

            range.Type = ValidationDataType.Date;
            range.Display = ValidatorDisplay.Dynamic;
            range.ID = string.Format("range_{0}_{1}", element.FormID, element.ID);
            range.ControlToValidate = textbox.ID;
            range.ValidationGroup = element.FormID.ToString();

            if (element.MinValue.HasValue)
                range.MinimumValue = element.MinValue.Value.ToString();
            else
                range.MinimumValue = DateTime.MinValue.ToShortDateString();

            if (element.MaxValue.HasValue)
                range.MaximumValue = element.MaxValue.ToString();
            else
                range.MaximumValue = DateTime.MaxValue.ToShortDateString();

            range.ErrorMessage = string.Format("{0} the date must be between {1} and {2}", element.Label, range.MinimumValue, range.MaximumValue);

            phValidators.Controls.Add(range);

            #endregion

            GenerateRow(element, label, calendarControls, phValidators);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ImageButton button = this.FindControl(string.Format("button_calendar_{0}", this.ID)) as ImageButton;
            button.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Alogient.Cameleon.Forms.Sdk.Resources.CalendarIcon.gif");
        }
    }
}
